package xcmg.device.service.report;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.report.LocationReportDO;
import xcmg.device.dao.mapper.report.LocationReportMapper;
import xcmg.device.infra.CellMergeParam;
import xcmg.device.infra.ExportUtil;
import xcmg.device.service.vo.report.LocationReportConditionVO;
import xcmg.device.service.vo.report.LocationReportVO;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class LocationReportService {

    @Autowired
    private LocationReportMapper locationReportMapper;
    @Autowired
    private HttpServletResponse response;

    public PageCommonVO pageList(SearchCommonVO<LocationReportConditionVO> conditionVO, String orgId) {

        conditionVO.getFilters().setOrgId(orgId);

        PageHelperPlus.startPage(conditionVO.getPageNum(), conditionVO.getPageSize());
        List<LocationReportDO> locationReportDOs = locationReportMapper.list(conditionVO.getFilters());
        PageCommonVO pageCommonVO = new PageCommonVO();
        pageCommonVO.setPageInfo(new PageInfo(locationReportDOs));
        List<LocationReportVO> locationReportVOs = new ArrayList<LocationReportVO>();
        for (LocationReportDO locationReportDO : locationReportDOs) {
            if (locationReportDO.getPreAmount() != null) {
                locationReportDO.setPreAmount(locationReportDO.getPreAmount().divide(new BigDecimal(100)));
            }
            if (locationReportDO.getInAmount() != null) {
                locationReportDO.setInAmount(locationReportDO.getInAmount().divide(new BigDecimal(100)));
            }
            if (locationReportDO.getOutAmount() != null) {
                locationReportDO.setOutAmount(locationReportDO.getOutAmount().divide(new BigDecimal(100)));
            }
            if (locationReportDO.getAfterAmount() != null) {
                locationReportDO.setAfterAmount(locationReportDO.getAfterAmount().divide(new BigDecimal(100)));
            }
            LocationReportVO locationReportVO = new LocationReportVO();
            BeanUtils.copyProperties(locationReportDO, locationReportVO);
            locationReportVOs.add(locationReportVO);
        }
        if (CollectionUtils.isNotEmpty(locationReportVOs)) {
            LocationReportDO totalInfoDO = locationReportMapper.getTotalInfo(conditionVO.getFilters());
            if (totalInfoDO.getPreAmount() != null) {
                totalInfoDO.setPreAmount(totalInfoDO.getPreAmount().divide(new BigDecimal(100)));
            }
            if (totalInfoDO.getInAmount() != null) {
                totalInfoDO.setInAmount(totalInfoDO.getInAmount().divide(new BigDecimal(100)));
            }
            if (totalInfoDO.getOutAmount() != null) {
                totalInfoDO.setOutAmount(totalInfoDO.getOutAmount().divide(new BigDecimal(100)));
            }
            if (totalInfoDO.getAfterAmount() != null) {
                totalInfoDO.setAfterAmount(totalInfoDO.getAfterAmount().divide(new BigDecimal(100)));
            }
            LocationReportVO totalInfoVO = new LocationReportVO();
            BeanUtils.copyProperties(totalInfoDO, totalInfoVO);
            locationReportVOs.add(totalInfoVO);
        }
        pageCommonVO.setPageInfoList(locationReportVOs);
        return pageCommonVO;
    }

    public void export(LocationReportConditionVO conditionVO, String orgId) throws Exception {
        conditionVO.setOrgId(orgId);
        List<LocationReportDO> locationReportDOs = locationReportMapper.list(conditionVO);
        exportLocationReport(locationReportDOs);
    }

    private void exportLocationReport(List<LocationReportDO> locationReportDOs) throws Exception {
        String title = "备件库存库位汇总表";
        String[] subRowName = new String[]{"序号", "仓库", "库区", "期初库存", "期初库存", "入库", "入库", "出库", "出库", "期末入库", "期末入库"};
        String[] rowsName = new String[]{"序号", "仓库", "库区", "数量", "成本合计", "数量", "成本合计", "数量", "成本合计", "数量", "成本合计"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        dataList.add(subRowName);
        dataList.add(rowsName);
        Object[] objs;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        BigDecimal preQuantity = BigDecimal.ZERO;
        BigDecimal preAmount = BigDecimal.ZERO;
        BigDecimal inQuantity = BigDecimal.ZERO;
        BigDecimal inAmount = BigDecimal.ZERO;
        BigDecimal outQuantity = BigDecimal.ZERO;
        BigDecimal outAmount = BigDecimal.ZERO;
        BigDecimal afterQuantity = BigDecimal.ZERO;
        BigDecimal afterAmount = BigDecimal.ZERO;
        for (int i = 0; i < locationReportDOs.size(); i++) {
            LocationReportDO reportDO = locationReportDOs.get(i);
            if (reportDO.getPreAmount() != null) {
                reportDO.setPreAmount(reportDO.getPreAmount().divide(new BigDecimal(100)));
            }
            if (reportDO.getInAmount() != null) {
                reportDO.setInAmount(reportDO.getInAmount().divide(new BigDecimal(100)));
            }
            if (reportDO.getOutAmount() != null) {
                reportDO.setOutAmount(reportDO.getOutAmount().divide(new BigDecimal(100)));
            }
            if (reportDO.getAfterAmount() != null) {
                reportDO.setAfterAmount(reportDO.getAfterAmount().divide(new BigDecimal(100)));
            }
            objs = new Object[rowsName.length];
            objs[0] = i + 1;
            objs[1] = reportDO.getWarehouseCode();
            objs[2] = reportDO.getAreaCode();
            objs[3] = reportDO.getPreQuantity();
            objs[4] = reportDO.getPreAmount();
            objs[5] = reportDO.getInQuantity();
            objs[6] = reportDO.getInAmount();
            objs[7] = reportDO.getOutQuantity();
            objs[8] = reportDO.getOutAmount();
            objs[9] = reportDO.getAfterQuantity();
            objs[10] = reportDO.getAfterAmount();
            dataList.add(objs);
            preQuantity = preQuantity.add(reportDO.getPreQuantity()==null?BigDecimal.ZERO:reportDO.getPreQuantity());
            preAmount = preAmount.add(reportDO.getPreAmount()==null?BigDecimal.ZERO:reportDO.getPreAmount());
            inQuantity = inQuantity.add(reportDO.getInQuantity()==null?BigDecimal.ZERO:reportDO.getInQuantity());
            inAmount = inAmount.add(reportDO.getInAmount()==null?BigDecimal.ZERO:reportDO.getInAmount());
            outQuantity = outQuantity.add(reportDO.getOutQuantity()==null?BigDecimal.ZERO:reportDO.getOutQuantity());
            outAmount = outAmount.add(reportDO.getOutAmount()==null?BigDecimal.ZERO:reportDO.getOutAmount());
            afterQuantity = afterQuantity.add(reportDO.getAfterQuantity()==null?BigDecimal.ZERO:reportDO.getAfterQuantity());
            afterAmount = afterAmount.add(reportDO.getAfterAmount()==null?BigDecimal.ZERO:reportDO.getAfterAmount());
        }
        objs = new Object[rowsName.length];
        objs[0] = locationReportDOs.size()+1;
        objs[1] = "合计";
        objs[2] = "合计";
        objs[3] = preQuantity;
        objs[4] = preAmount;
        objs[5] = inQuantity;
        objs[6] = inAmount;
        objs[7] = outQuantity;
        objs[8] = outAmount;
        objs[9] = afterQuantity;
        objs[10] = afterAmount;
        dataList.add(objs);

        List<CellMergeParam> cellMergeParams = new ArrayList<CellMergeParam>();

        CellMergeParam cellMergeParam = new CellMergeParam(dataList.size(), dataList.size(), 1, 2);
        cellMergeParams.add(cellMergeParam);

        ExportUtil exportUtil = new ExportUtil(title, null, dataList);
        exportUtil.exportWithoutTitles(response, cellMergeParams);
    }
}
